# encoding=utf-8
import requests
from common.readYaml import read_data
from config.read_config import Config
from settings.settings import Route


class Request:

    def __init__(self):

        self.domain = Config().read(Route().config('router.ini'), 'baseUrl', 'url')

    def request(self, filename):
        """

        :param filename: testcases目录下文件中的用例
        :return: 请求接口的响应数据（response）
        """
        # 读取testcases 目录下的文件的用例
        yamlData = read_data(Route().testcases(filename))

        url = self.domain + yamlData['request_url']
        data = yamlData['request_param']
        headers = yamlData['request_header']
        # 请求数据格式
        data_format = yamlData['data_format']
        # 断言
        assert_condition = yamlData['assert_condition']
        print(yamlData)
        if yamlData['request_method'] == "get":
            res = requests.get(url=url, params=data, headers=headers)
            result = res.json()
            print(result)

        else:
            if yamlData['data_format'] == "0":
                res = requests.post(url=url, data=data, headers=headers)
                result = res.json()

            else:
                res = requests.post(url.url, json=data, headers=headers)
                result = res.json()

            return result
        return result

    # def request_assert(self, filename):
    #     result = self.request(filename)
    #     if result['message'] == read_data(Route().testcases(filename))['assert_condition']:
    #         print('断言成功')
    #     else:
    #         print('断言失败')


if __name__ == '__main__':
    pass
